package com.jjlin.chapter_1;

import java.util.Scanner;
import java.util.Stack;

//设计一个getMin功能的栈
//题目链接：https://www.nowcoder.com/practice/05e57ce2cd8e4a1eae8c3b0a7e9886be

//方法一：
public class CD5 {
    public Stack<Integer> dataStack;//数据栈
    public Stack<Integer> minStack;//最小栈
    public CD5(){
        dataStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(Integer num){
        if(minStack.isEmpty())
            minStack.push(num);
        else{
            if(minStack.peek() < num)
                minStack.push(minStack.peek());
            else
                minStack.push(num);
        }
        dataStack.push(num);
    }

    public int pop(){
        if(minStack.isEmpty() && dataStack.isEmpty())
            throw new RuntimeException("stack is empty!");
        minStack.pop();
        return dataStack.pop();
    }

    public int getMin(){
        if(minStack.isEmpty())
            throw new RuntimeException("stack is empty!");
        return minStack.peek();
    }

    public static void main(String[] args){
        CD5 main = new CD5();
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        String[] str = new String[count];
        sc.nextLine();
        for(int i = 0; i < count; i++)
            str[i] = sc.nextLine();
        for(int i = 0; i < count; i++){
            String[] line = str[i].split(" ");
            if("push".equals(line[0])){
                Integer num = Integer.parseInt(line[1]);
                main.push(num);
            }else if("pop".equals(line[0])){
                main.pop();
            }else
                System.out.println(main.getMin());
        }
    }
}

//方法二：
/*
public class Main{

    public Stack<Integer> dataStack;//数据栈
    public Stack<Integer> minStack;//最小栈
    public Main(){
        dataStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(Integer num){
        if(minStack.isEmpty())
            minStack.push(num);
        else if(minStack.peek() >= num)//小于等于minStack栈顶元素才放入
            minStack.push(num);
        dataStack.push(num);
    }

    public int pop(){
        if(minStack.isEmpty() && dataStack.isEmpty())
            throw new RuntimeException("stack is empty!");
        if(dataStack.peek() == minStack.peek())
            minStack.pop();
        return dataStack.pop();
    }

    public int getMin(){
        if(minStack.isEmpty())
            throw new RuntimeException("stack is empty!");
        return minStack.peek();
    }

    public static void main(String[] args){
        Main stack = new Main();
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        String[] str = new String[count];
        sc.nextLine();
        for(int i = 0; i < count; i++)
            str[i] = sc.nextLine();
        for(int i = 0; i < count; i++){
            String[] line = str[i].split(" ");
            if("push".equals(line[0])){
                Integer num = Integer.parseInt(line[1]);
                stack.push(num);
            }else if("pop".equals(line[0])){
                stack.pop();
            }else
                System.out.println(stack.getMin());
        }
    }
}
*/